<?php

require ("sharedResources.php");

$sid = '';
try {
    // We need a SID (session ID) in order to operate
	$sid = login ( 'admin', 'admin' );
	echo 'sid: ' . $sid . PHP_EOL;
	
	// 1) First invoke the method getDocument to get the filename
	$docId = 6553601;
	
	$ddi = array ('sid' => $sid, 'docId' => $docId );
	$result = $documentClient->getDocument ( $ddi );
	print_r ( $result );
	
	$myFileName = $result->document->fileName;
	print_r ( 'fileName: '.$myFileName .PHP_EOL);
	
	// 2) Then download the document (binary content)
	$ddp = array ('sid' => $sid, 'docId' => $docId );
	
	$result = $documentClient->getContent ( $ddp );
	
	// get the binary content from the result response
	$content = $result->return;
	
	// Finally write the document on disk
	$myFile = "C:\\tmp\\" . $myFileName;
	echo 'Writing content to: ' .$myFile .PHP_EOL;
	
	$fh = fopen ( $myFile, 'w' ) or die ( "can't open file" );
	fwrite ( $fh, $content );
	fclose ( $fh );

} catch ( SoapFault $e ) {
	print_r ( $e );
}

// Always we need to logout in order to release connection slot
logout ( $sid );
?> 
